package com.example.test.mapper;

import com.example.test.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {

    @Insert("INSERT INTO products(name, image, description, quantity, price, ifground, sellerUsername) " +
            "VALUES(#{name}, #{image}, #{description}, #{quantity}, #{price}, #{ifground}, #{sellerUsername})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertProduct(Product product);

    @Select("SELECT * FROM products")
    List<Product> getAllProducts();

    @Update("UPDATE products SET name=#{name}, image=#{image}, description=#{description}, " +
            "quantity=#{quantity}, price=#{price}, ifground=#{ifground}, sellerUsername=#{sellerUsername} " +
            "WHERE id=#{id}")
    void updateProduct(Product product);

    @Delete("DELETE FROM products WHERE id=#{id}")
    void deleteProduct(Long id);

    @Select("SELECT * FROM products WHERE id=#{id}")
    Product getProductById(Long id);
    @Select("select * from products")
    List<Product> selectAll();
    @Update("UPDATE products SET quantity=#{quantity} WHERE id=#{id} ")
    void updatequantity(Long id, Integer quantity);
    @Select("select * from  products WHERE quantity!=0 AND ifground=1")
    List<Product> check();
    @Mapper

    @Update("UPDATE products SET quantity = quantity + #{quantity} WHERE id = #{productId}")
    void updateProductQuantity(@Param("productId") Long productId, @Param("quantity") int quantity);

}
